package com.dx.architectureddd.domain.component.flow;

import com.dx.architectureddd.client.dto.event.GameOverEvent;
import com.dx.architectureddd.common.event.impl.DomainSpringEventPublisher;
import com.dx.architectureddd.domain.FlowNodeContext;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * 流程节点完成，触发相关事件，解耦依赖
 *
 * @author xin.deng
 * @date 2023/10/05
 */
@Slf4j
@LiteflowComponent("NodeFinishTriggerCmp")
public class NodeFinishTriggerCmp extends NodeComponent {

    @Resource
    private DomainSpringEventPublisher domainSpringEventPublisher;

    @Override
    public void process() {
        FlowNodeContext contextBean = this.getContextBean(FlowNodeContext.class);
        String tag = this.getTag();
        if (contextBean.getReward() != null) {
            domainSpringEventPublisher.publishEvent(new GameOverEvent(contextBean.getReward()));
        } else {
            log.info("Game Over => {}", tag);
        }
    }
}
